<template>
  <a-modal
    v-model:visible="visible"
    :title="title"
    @ok="saveInfo"
    @close="closeModal"
  >
    <a-form :model="form">
      <a-form-item field="name" label="分组名称">
        <a-input v-model="form.name" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { Message } from '@arco-design/web-vue';

import { ref } from 'vue';
import { saveTempInfo } from '@/api/produce';
const props = defineProps({
  getWorkshop: {
    type: Function,
    default: () => {},
  },
});
const visible = ref(false);
const form = ref({
  name: '',
});
const saveData = ref({
  workshop_id: '',
  group_id: '',
  group_alias: '',
  temperatureAlias: '',
});
const title = ref('新增数据');
const openModal = (record: any) => {
  visible.value = true;
  saveData.value.workshop_id = record.workshop_id;
  if (record.group_id) {
    saveData.value.group_id = record.group_id;
    title.value = '新增品温';
  } else {
    title.value = '新增分组';
  }
};
const saveInfo = async () => {
  if (saveData.value.group_id) {
    saveData.value.temperatureAlias = form.value.name;
  } else {
    saveData.value.group_alias = form.value.name;
  }
  const { data, code, message } = await saveTempInfo(saveData.value);
  if (code === 200) {
    Message.success(message);
    props.getWorkshop(); // 重新获取数据
  }
};
const closeModal = () => {
  visible.value = false;
  form.value.name = '';
  saveData.value.group_alias = '';
  saveData.value.temperatureAlias = '';
  saveData.value.group_id = '';
  saveData.value.workshop_id = '';
};
defineExpose({ openModal });
</script>